<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2017/1/18
 * Time: 11:56
 */
class PurseAction extends Action{
    //查看使用过充值卡的人的钱包信息
    public function getPurseList($name=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $purseModel = M("module_user_purse");
        Session::switchCustomer($customerId);
        $where['a.cid']=array("eq",$customerId);
        $name?$where['_string']=" b.nickname like '%$name%' or b.fullName like '%$name%'":"";
        $arr = $purseModel->alias("a")
            ->join("wx_".$customerId."_user_weixin_info b on a.userId=b.userId")
            ->join('wx_' . $customerId . '_user u on u.id = a.userId')
            ->field("a.*,b.nickname,b.headimgurl,u.fullName,u.phone")
            ->where($where)
            ->limit($start,$limit)
            ->order("a.updateTime desc")
            ->select();
        if($arr){
            $count = $purseModel->alias("a")
                ->join("wx_".$customerId."_user_weixin_info b on a.userId=b.id")
                ->where($where)
                ->count();
            Output::instance()->objArray($arr,$count);
        }else{
            $data['results'] = [];
            $data['count'] = 0;
            Output::instance()->object($data);
        }
    }
    //账单列表
    public function billList($id,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $purseModel = M("module_user_purse_bill");
        Session::switchCustomer($customerId);
        $where['userId']=array("eq",$id);
        $where['cid']=array("eq",$customerId);
        $where['deleted']=array("eq",0);
        $arr = $purseModel->where($where)->limit($start,$limit)->order("createTime desc")->select();
        if($arr){
            $count = $purseModel->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }

    public function getPurseUrl()
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $customerId = Session::$user["customerId"];
        $strUrl2 = "call.html?customerId=$customerId&module=purse&method=index";
        $strUrl1 = R('MiContent/getH5ServerHost');
        $url = $strUrl1.$strUrl2;
        Output::instance()->object([
            'code' => 0,
            'data' => $url
        ]);
    }

    public function getPhoneUrl($module, $method, $param = '')
    {
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $param = urlencode($param);
        $customerId = Session::$user["customerId"];
        $strUrl2 = "call.html?customerId=$customerId&module=$module&method=$method";
        (empty($param)) ? $strUrl3 = '':$strUrl3 = '&params=' . $param;
        $strUrl1 = R('MiContent/getH5ServerHost');
        $url = $strUrl1.$strUrl2.$strUrl3;

        return $url;
    }

}